import { Injectable } from '@nestjs/common';
import { PrismaService } from 'src/common/prisma/PrismaService';
import ExamPlanQueryParam from '../param/ExamPlanQueryParam';
import ExamPlanDto from '../dto/ExamPlanDto';
import ExamPlanVo from '../vo/ExamPlanVo';
import { plainToInstance } from 'class-transformer';
import { Util } from 'src/common/Util';
import ServiceException from 'src/common/ServiceException';

/*
 * @Author: jingfuu@163.com
 * @Date: 2024-12-16 13:22:46
 * @LastEditTime: 2024-12-16 14:11:29
 * @Description: 考试计划service
 */
@Injectable()
class ExamPlanService {
  constructor(private prisma: PrismaService) {}

  async remove(id: number) {
    await this.prisma.examPlan.delete({ where: { id } });
  }
  async edit(data: ExamPlanDto) {
    await this.checkExamPlanName(data.name, data.id);
    await this.prisma.examPlan.update({
      data: {
        name: data.name,
        startTime: Util.convertUTCToLocalTime(data.startTime),
        endTime: Util.convertUTCToLocalTime(data.endTime),
        examFlag: data.examFlag,
        ticketPrintFlag: data.ticketPrintFlag,
        printStartTime: Util.convertUTCToLocalTime(data.printStartTime),
        printEndTime: Util.convertUTCToLocalTime(data.printEndTime),
      },
      where: {
        id: data.id,
      },
    });
  }
  async checkExamPlanName(name: string, id: number) {
    const examPlan = await this.prisma.examPlan.findFirst({
      where: {
        name: name,
      },
    });
    if (!id) {
      if (examPlan) {
        throw new ServiceException('考试计划：' + name + '已存在');
      }
    } else {
      if (examPlan && examPlan.id != id) {
        throw new ServiceException('考试计划：' + name + '已存在');
      }
    }
  }
  async add(data: ExamPlanDto) {
    await this.checkExamPlanName(data.name, data.id);
    await this.prisma.examPlan.create({
      data: {
        name: data.name,
        startTime: Util.convertUTCToLocalTime(data.startTime),
        endTime: Util.convertUTCToLocalTime(data.endTime),
        examFlag: data.examFlag,
        ticketPrintFlag: data.ticketPrintFlag,
        printStartTime: Util.convertUTCToLocalTime(data.printStartTime),
        printEndTime: Util.convertUTCToLocalTime(data.printEndTime),
      },
    });
  }
  async detail(id: number) {
    const examPlan = await this.prisma.examPlan.findUnique({
      where: { id },
    });
    return plainToInstance(ExamPlanVo, examPlan);
  }
  async list(param: ExamPlanQueryParam) {
    const skip = param.pageNum != 0 ? (param.pageNum - 1) * param.pageSize : 0;
    const whereClause: any = this.generatePageQueryCondition(param);
    const [total, list] = await this.prisma.$transaction([
      this.prisma.examRoom.count({ where: whereClause }),
      this.prisma.examRoom.findMany({
        skip,
        take: param.pageSize,
        orderBy: {
          createTime: 'desc',
        },
        where: whereClause,
      }),
    ]);
    const rows: ExamPlanVo[] = plainToInstance(ExamPlanVo, list);
    return {
      total,
      rows,
    };
  }
  generatePageQueryCondition(param: ExamPlanQueryParam): any {
    const whereClause: any = {};
    if (param.name) {
      whereClause.name = {
        contains: param.name,
      };
    }
    return whereClause;
  }
}
export default ExamPlanService;
